package com.xm.xmapp;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type.F_ShortTimeOrder;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.CustomLayout;
import com.xm.xmapp.customview.PinJiaTipDialog;
import com.xm.xmapp.fragment.ShortTimeOrderFragment;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MyDialogButton;
import com.xm.xmapp.request.bean.Get_order_comment_infoRequest;
import com.xm.xmapp.request.bean.Get_user_order_moneyRequest;
import com.xm.xmapp.response.bean.BaseBeanResponse;
import com.xm.xmapp.response.bean.OrderBeanResponse;
import com.xm.xmapp.response.bean.ResponseOrderCommentInfo;
import com.xm.xmapp.response.bean.ResponseUserOrderMoney;
import com.xm.xmapp.tools.StringTools;

@SuppressLint("SimpleDateFormat")
public class ShortTimeOrderDetailsActivity extends BaseActivity implements OnClickListener {

	private CustomLayout cl_car_on_money, cl_car_off_money, cl_discount_amount, cl_pay_money, cl_add_time,
			cl_order_number, cl_order_paytype, cl_return_time, cl_day_times, cl_day_money, cl_today_max_money, fuwu,
			suodan;
	private TextView status, miaoshu, mycomment, name, address;
	private OrderBeanResponse orderbean;
	private LinearLayout lin;
	private RelativeLayout shifu;
	private TextView shifumoney;
	private ImageView img[] = new ImageView[5];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_orderdetails);

	}

	@Override
	public void initView() {

		cl_car_on_money = (CustomLayout) findViewById(R.id.cl_car_on_money);
		cl_car_off_money = (CustomLayout) findViewById(R.id.cl_car_off_money);
		cl_discount_amount = (CustomLayout) findViewById(R.id.cl_discount_amount);
		cl_pay_money = (CustomLayout) findViewById(R.id.cl_pay_money);
		cl_add_time = (CustomLayout) findViewById(R.id.cl_add_time);
		cl_order_number = (CustomLayout) findViewById(R.id.cl_order_number);
		cl_order_paytype = (CustomLayout) findViewById(R.id.cl_order_paytype);
		cl_return_time = (CustomLayout) findViewById(R.id.cl_return_time);
		cl_day_times = (CustomLayout) findViewById(R.id.cl_day_times);
		cl_day_money = (CustomLayout) findViewById(R.id.cl_day_money);
		cl_today_max_money = (CustomLayout) findViewById(R.id.cl_today_max_money);
		fuwu = (CustomLayout) findViewById(R.id.fuwu);
		suodan = (CustomLayout) findViewById(R.id.suodan);
		status = (TextView) findViewById(R.id.status);
		name = (TextView) findViewById(R.id.name);

		miaoshu = (TextView) findViewById(R.id.miaoshu);
		address = (TextView) findViewById(R.id.address);
		mycomment = (TextView) findViewById(R.id.mycomment);
		lin = (LinearLayout) findViewById(R.id.lin);
		shifu = (RelativeLayout) findViewById(R.id.shifu);
		shifumoney = (TextView) findViewById(R.id.shifumoney);

		img[0] = (ImageView) findViewById(R.id.img0);
		img[1] = (ImageView) findViewById(R.id.img1);
		img[2] = (ImageView) findViewById(R.id.img2);
		img[3] = (ImageView) findViewById(R.id.img3);
		img[4] = (ImageView) findViewById(R.id.img4);

	}

	@Override
	public void initData() {

		setMyTitle("订单详情");
		orderbean = (OrderBeanResponse) getIntent().getSerializableExtra("orderbean");
		address.setText(orderbean.add_network_name
				+ ((orderbean.return_network_name == null || orderbean.return_network_name.equals("")) ? ""
						: (" — " + orderbean.return_network_name)));

		if (!orderbean.order_status.equals("2") && !orderbean.order_status.equals("5")) {
			shifu.setVisibility(View.VISIBLE);
			name.setText(orderbean.car_number);
			int time = Integer.parseInt(orderbean.car_on_minute_today_total);
			cl_car_on_money.setText2((((time / 60 % 24) == 0 ? "" : (time / 60 % 24) + "小时") + time % 60 + "分钟")
					+ "    " + orderbean.car_on_money_today_total + "元");

			if (time == 0) {
				cl_car_on_money.setText2("无计费");
			}
			time = Integer.parseInt(orderbean.car_off_minute_today_total);
			cl_car_off_money.setText2((((time / 60 % 24) == 0 ? "" : (time / 60 % 24) + "小时") + time % 60 + "分钟")
					+ "    " + orderbean.car_off_money_today_total + "元");
			if (time == 0) {
				cl_car_off_money.setText2("无计费");
			}

			cl_discount_amount.setText2(orderbean.discount_amount + "元");
			cl_pay_money.setText2(orderbean.pay_money + "元");
			shifumoney.setText(orderbean.pay_money + "元");
			cl_add_time.setText2(StringTools.getTime(orderbean.add_time * 1000, "yyyy-MM-dd HH:mm:ss"));
			cl_order_number.setText2(orderbean.order_sn);
			cl_order_paytype.setText2(StringTools.getPayWay(orderbean.pay_way_type));
			cl_return_time.setText2(StringTools.getTime(orderbean.return_time * 1000, "yyyy-MM-dd HH:mm:ss"));
			cl_day_times.setText2(orderbean.day_times + "天");
			cl_today_max_money.setText2(orderbean.today_max_money + "元");
		}
		cl_day_money.setText2(orderbean.day_money_total + "元");
		if (orderbean != null && orderbean.lock_minute_total != 0) {
			suodan.setVisibility(View.VISIBLE);
			suodan.setText2(StringTools.getStringTime(orderbean.lock_minute_total));
		} else {
			suodan.setVisibility(View.GONE);
		}
		if (orderbean.send_car_service_money == 0) {
			fuwu.setVisibility(View.GONE);
			findViewById(R.id.fuwuview).setVisibility(View.GONE);
		}
		fuwu.setText2(StringTools.formatFloatNumberMoney(orderbean.send_car_service_money) + "元");

		if (orderbean.order_status.equals("2") || orderbean.order_status.equals("5")) {
			shifu.setVisibility(View.GONE);
			cl_pay_money.setVisibility(View.VISIBLE);

			Get_user_order_moneyRequest requestBean = new Get_user_order_moneyRequest();
			requestBean.order_id = orderbean.order_id;
			requestBean.sim = orderbean.car_tbox_sim;
			promptDialog.show();
			MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

				@Override
				public void onResponse(String response) {
					promptDialog.dismiss();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String date = sdf.format(new Date(orderbean.add_time * 1000));
					cl_add_time.setText2(date + "");
					cl_order_number.setText2(orderbean.order_sn);

					BaseBeanResponse<String> baseBean = JSON.parseObject(response,
							new TypeReference<BaseBeanResponse<String>>() {
							});

					if (baseBean.errCode == 10004) {

					} else if (baseBean.errCode == 0) {
						ResponseUserOrderMoney base = JSON.parseObject(baseBean.data, ResponseUserOrderMoney.class);
						name.setText(orderbean.car_number);
						if (base.orderMoneyInfoArr.send_car_service_money == 0) {
							fuwu.setVisibility(View.GONE);
							findViewById(R.id.fuwuview).setVisibility(View.GONE);
						}

						fuwu.setText2(StringTools.formatFloatNumberMoney(base.orderMoneyInfoArr.send_car_service_money)
								+ "元");
						cl_pay_money.setText2(base.money + "元");
						cl_day_times.setText2(base.orderMoneyInfoArr.today_money_times + "天");
						cl_day_money.setText2(base.orderMoneyInfoArr.today_money_total + "元");
						cl_today_max_money.setText2(base.orderMoneyInfoArr.today_max_money + "元");

						int hour = Integer.parseInt(base.orderMoneyInfoArr.car_on_minute_total) / 60;
						int min = Integer.parseInt(base.orderMoneyInfoArr.car_on_minute_total) % 60;
						cl_car_on_money.setText2((hour % 24 == 0 ? "" : (hour % 24 + "小时")) + min + "分钟" + " "
								+ base.orderMoneyInfoArr.car_on_money_total + "元");

						hour = Integer.parseInt(base.orderMoneyInfoArr.car_off_minute_total) / 60;
						min = Integer.parseInt(base.orderMoneyInfoArr.car_off_minute_total) % 60;
						cl_car_off_money.setText2((hour % 24 == 0 ? "" : (hour % 24 + "小时")) + min + "分钟   "
								+ base.orderMoneyInfoArr.car_off_money_total + "元");
						if (base.orderMoneyInfoArr.lock_minute_total != 0) {
							suodan.setVisibility(View.VISIBLE);
							suodan.setText2(StringTools.getStringTime(base.orderMoneyInfoArr.lock_minute_total));
						} else {
							suodan.setVisibility(View.GONE);
						}
					}
				}
			}, new DisssmissInterFace() {

				@Override
				public void putError(VolleyError error) {
					promptDialog.dismiss();
				}
			});
		} else if (orderbean != null && !orderbean.order_status.equals("2")) {
			cl_pay_money.setVisibility(View.GONE);
		}

	}

	@Override
	public void initEvent() {

		status.setOnClickListener(this);
		setStatus();

	}

	@SuppressWarnings("deprecation")
	private void setStatus() {
		status.setClickable(false);
		suodan.setVisibility(View.GONE);
		switch (orderbean.order_status) {
		case "1":
			status.setText("已下单");
			break;
		case "2":
			status.setText("用车中");
			cl_discount_amount.setVisibility(View.GONE);
			cl_return_time.setVisibility(View.GONE);
			cl_order_paytype.setVisibility(View.GONE);
			break;
		case "3":
			status.setText("已还车");
			if (orderbean.is_comment.equals("0")) {
				status.setTextColor(ContextCompat.getColor(this, R.color.red_text_color));
				status.setBackgroundResource(R.drawable.line_red_bg_while);
				status.setText("去评价");
				status.setClickable(true);

			} else {
				status.setTextColor(ContextCompat.getColor(this, R.color.color6));
				status.setBackgroundDrawable(null);
			}
			break;
		case "4":
			status.setText("已取消");
			break;
		case "5":
			status.setText("锁定");
			cl_discount_amount.setVisibility(View.GONE);
			cl_return_time.setVisibility(View.GONE);
			cl_order_paytype.setVisibility(View.GONE);
			if (orderbean != null && orderbean.lock_minute_total != 0) {
				suodan.setVisibility(View.VISIBLE);
				suodan.setText2(StringTools.getStringTime(orderbean.lock_minute_total));
			}
			break;
		default:
			status.setText("锁定");
			break;
		}
		if (!orderbean.order_status.equals("3")) {
			status.setTextColor(ContextCompat.getColor(this, R.color.color6));
			status.setBackgroundDrawable(null);

		}
		if (orderbean.is_comment.equals("1")) {
			status.setClickable(false);
			get_order_comment_infoRequest();
		}

	}

	private void get_order_comment_infoRequest() {

		Get_order_comment_infoRequest requestBean = new Get_order_comment_infoRequest();
		requestBean.order_id = orderbean.order_id;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				try {
					promptDialog.dismiss();
					ResponseOrderCommentInfo base = JSON.parseObject(response, ResponseOrderCommentInfo.class);
					setImg(base);
					lin.setVisibility(View.VISIBLE);

				} catch (Exception e) {

				}

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

				promptDialog.dismiss();
			}
		});

	}

	private void setImg(ResponseOrderCommentInfo base) {
		for (int i = 0; i < img.length; i++) {
			if ((i + 1) <= base.order_star) {
				img[i].setImageResource(R.mipmap.xing_icon_dis);
			} else {
				img[i].setImageResource(R.mipmap.xing_icon_nor);
			}
		}
		miaoshu.setText(StringTools.getCommentText(base.order_star));
		mycomment.setText(base.content);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.status:

			new PinJiaTipDialog(this, orderbean, new MyDialogButton() {

				@Override
				public void setTitle(int flag, String str) {

					orderbean.is_comment = "1";
					orderbean.order_status = "3";
					setStatus();
					MyAppcation.getMyAppcation().notifyDataSetChanged(ShortTimeOrderFragment.class,F_ShortTimeOrder.DEFAULT);

				}
			}).show();

			break;
		case R.id.back:
			finish();
			break;
		default:
			break;
		}
	}

}
